<?php

/**
 * DealOrderController is the default controller for this application
 */
class DealOrderController extends ControllerBase {
	
	/**
	 * Admin DealOrder Data Service Object
	 * @var dealOrderDataService object
	 */
	private $grouponDataService;
	private $dealDataService;
	private $dealOrderDataService;
	public $grouponCategoryDataService = null;
	/**
	 *
	 * UsersDataService Object
	 * @var UsersDataService object
	 */
	public $userDataService = null;
	/**
	 * DealOrderController Constructor
	 */
	public $orderAddressDataService = null;
	public function __construct() {
		parent::__construct ();
		
		// Set dealOrder name
		$this->setPageName ( 'admin_dealOrder' );
		
		// Init Authentication class
		$authentication = new Authentication ();
		
		// Only administrator can right
		if (! ($authentication->isAdministrator () == true || $authentication->getUserType () == ROLE_ACCOUNTANT)) {
			// Go to login dealOrder
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/';
			CommonUtils::redirect ( $url );
		}
		
		// Get status list of dealOrder category
		//		$this->smarty->assign('statusList', DealOrderEntity::getStatusList());
		

		// New groupon Data Service
		$this->grouponDataService = new GrouponDataService ();
		// new dealOrder category Data Service
		$this->grouponCategoryDataService = new GrouponCategoryDataService ();
		// New dealOrder Data Service
		$this->dealOrderDataService = new DealOrderDataService ();
		// new deal
		$this->dealDataService = new DealsDataService ();
		//address
		$this->orderAddressDataService = new OrderAddressDataService ();
		$this->smarty->assign ( 'pageName', 'order' );
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing dealOrder of the site. Return list of all dealOrder category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function indexAction() {
		$arrElement = array ();
		$arrElementAll = array ();
		$status = - 1;
		$order_status = $this->getQuery ( 'status' );
		if ($order_status != '') {
			$arrElement ['order_status'] = $this->getQuery ( 'status', 0 );
			$arrElementAll ['OD.order_status'] = $this->getQuery ( 'status', 0 );
			$status = $this->getQuery ( 'order_status', 0 );
		}
		$this->smarty->assign ( 'submenu', $order_status );
		$where = '';
		
		if ($this->getPost ( 'submit' ) == 'Tìm') {
			if ($this->getPost ( 'groupon_name' ) != '') {
				$where .= ' AND G.groupon_name LIKE "%' . $this->getPost ( 'groupon_name' ) . '%"';
			}
			$this->smarty->assign ( 'groupon_name', $this->getPost ( 'groupon_name' ) );
			if ($this->getPost ( 'started_deal' ) != '') {
				$where .= ' AND D.started_deal > ' . $this->getPost ( 'started_deal' );
			}
			$this->smarty->assign ( 'started_deal', $this->getPost ( 'started_deal' ) );
			if ($this->getPost ( 'expired_deal' ) != '') {
				$where .= ' AND D.expired_deal > ' . $this->getPost ( 'expired_deal' );
			}
			$this->smarty->assign ( 'expired_deal', $this->getPost ( 'expired_deal' ) );
			if ($this->getPost ( 'category' ) != '') {
				$where .= ' AND GC.id = ' . $this->getPost ( 'category' );
			}
			$this->smarty->assign ( 'category', $this->getPost ( 'category' ) );
			if ($this->getPost ( 'deal_status' ) != '-1') {
				$arrElement ['order_status'] = $this->getPost ( 'deal_status' );
				$where .= ' AND D.status = ' . $this->getPost ( 'deal_status' );
				$status = $this->getPost ( 'deal_status' );
			}
		
		}
		$this->smarty->assign ( 'deal_status', $status );
		$numRecordPerPage = ITEM_PER_PAGE;
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		// Get Totals Templates
		$totals = $this->dealOrderDataService->getTotals ( $arrElement );
		
		$itemList = $this->dealOrderDataService->getItemsAll ( $arrElement, 'OD.id desc', $start, $numRecordPerPage, $where );
		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Admin/DealOrder/Index/status/" . $status;
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'this', $this );
		$this->smarty->assign ( 'pageLink', $pageLink );
		$this->smarty->assign ( 'dealOrderEntity', new DealOrderEntity () );
		$this->smarty->assign ( 'dealStatusList', DealOrderEntity::getStatusList () );
		$this->smarty->assign ( 'categoryList', $this->grouponCategoryDataService->getItems ( array () ) );
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/DealOrder/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Dispplay List of dealOrder_category
	 * landing dealOrder of the site. Return list of all dealOrder_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function listAction() {
	
	}
	
	public function showorderAction() {
		//get dealOrder of Groupon
		$deal_id = $this->getQuery ( 'deal_id', 0 );
		$dealEntity = $this->dealDataService->getById ( $deal_id );
		$grouponEntity = $this->grouponDataService->getById ( $dealEntity->groupon_id );
		$orderList = $this->dealOrderDataService->getOrderOfDeal ( $deal_id );
		$this->smarty->assign ( 'orderList', $orderList );
		$this->smarty->assign ( 'dealEntity', $dealEntity );
		$this->smarty->assign ( 'grouponEntity', $grouponEntity );
		$this->smarty->assign ( 'dealOrderDataService', new DealOrderDataService () );
		$this->smarty->assign ( 'dealOrderEntity', new DealOrderEntity () );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/DealOrder/showorder.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	public function confirmAction() {
		$order_id = $this->getQuery ( 'id', 0 );
		$dealOrderEntity = $this->dealOrderDataService->getById ( $order_id );
		$orderAddressEntity = $this->orderAddressDataService->getById ( $dealOrderEntity->id );
		$dealEntity = $this->dealDataService->getById ( $dealOrderEntity->deal_id );
		$grouponEntity = $this->grouponDataService->getById ( $dealEntity->groupon_id );
		
		$this->smarty->assign ( 'dealOrderEntity', $dealOrderEntity );
		$this->smarty->assign ( 'orderAddressEntity', $orderAddressEntity );
		$this->smarty->assign ( 'dealEntity', $dealEntity );
		$this->smarty->assign ( 'grouponEntity', $grouponEntity );
		$this->smarty->display ( 'Admin/DealOrder/confirmOrder.tpl' );
	}
	public function cancelAction() {
		$order_id = $this->getQuery ( 'id', 0 );
		$this->smarty->assign ( 'order_id', $order_id );
		$this->smarty->display ( 'Admin/DealOrder/cancelOrder.tpl' );
	}
	
	public function doConfirmAction() {
		$action = $this->getPost ( 'submit', '' );
		$order_id = $this->getPost ( 'id', 0 );
		$dealOrderEntity = $this->dealOrderDataService->getById ( $order_id );
		//do confirm
		$dealOrderEntity->order_status = DealOrderEntity::CONFORMED;
		$this->dealOrderDataService->update ( $dealOrderEntity );
		// ++ bought count for deal
		$dealEntity = $this->dealDataService->getById ( $dealOrderEntity->deal_id );
		$dealEntity->bought_count = $dealEntity->bought_count + $dealOrderEntity->quantity;
		$this->dealDataService->update ( $dealEntity );
		$dealEntity = $this->dealDataService->getById ( $dealOrderEntity->deal_id );
		// update status of deal
		$dealEntity->updateStatus ();
		//if tipped
		if ($dealEntity->bought_count >= $dealEntity->min_bought) {
			if ($dealEntity->tipped_date == '0000-00-00 00:00:00') {
				$dealEntity = $this->dealDataService->getById ( $dealOrderEntity->deal_id );
				$dealEntity->tipped_date = new Zend_Db_Expr ( 'NOW()' );
				$this->dealDataService->update ( $dealEntity );
				$dealEntity = $this->dealDataService->getById ( $dealOrderEntity->deal_id );
			}
		
		// Update code for Buyer
		// $deal->updateCodeForBuyer();
		}
		
		echo Zend_Json::encode ( array ('success' => 1 ) );
		exit ();
	}
	public function doCancelAction() {
		$action = $this->getPost ( 'submit', '' );
		$order_id = $this->getPost ( 'id', 0 );
		$dealOrderEntity = $this->dealOrderDataService->getById ( $order_id );
		//do confirm
		$dealOrderEntity->order_status = DealOrderEntity::CANCELLED;
		$this->dealOrderDataService->update ( $dealOrderEntity );
		
		echo Zend_Json::encode ( array ('success' => 1 ) );
		exit ();
	}
	
	public function shippingAction() {
		$order_id = $this->getQuery ( 'id', 0 );
		$dealOrderEntity = $this->dealOrderDataService->getById ( $order_id );
		$orderAddressEntity = $this->orderAddressDataService->getById ( $dealOrderEntity->id );
		$dealEntity = $this->dealDataService->getById ( $dealOrderEntity->deal_id );
		$grouponEntity = $this->grouponDataService->getById ( $dealEntity->groupon_id );
		$userDataService = new UserDataService ();
		$userEntities = $userDataService->getUser ( array ('userType' => ROLE_SHIPPING ) );
		
		$shippingDate = date ( 'Y-m-d', strtotime ( '+2 days', strtotime ( date ( 'Y-m-d' ) ) ) );
		$this->smarty->assign ( 'shippingDate', $shippingDate );
		$this->smarty->assign ( 'dealOrderEntity', $dealOrderEntity );
		$this->smarty->assign ( 'orderAddressEntity', $orderAddressEntity );
		$this->smarty->assign ( 'dealEntity', $dealEntity );
		$this->smarty->assign ( 'grouponEntity', $grouponEntity );
		$this->smarty->assign ( 'userEntities', $userEntities );
		$this->smarty->display ( 'Admin/DealOrder/shippingOrder.tpl' );
	}
	public function doShippingAction() {
		$order_id = $this->getPost ( 'deal_order_id', 0 );
		$dealOrderEntity = $this->dealOrderDataService->getById ( $order_id );
		try {
			//do shipping
			$dealOrderEntity->order_status = DealOrderEntity::DELIVERY;
			$this->dealOrderDataService->update ( $dealOrderEntity );
			//do add order_shipping
			$orderShippingDataService = new OrderShippingDataService ();
			$orderShippingEntity = $orderShippingDataService->beforeCreate ();
			$orderShippingEntity->user_id = $this->getPost ( 'user_shipping' );
			$orderShippingEntity->deal_order_id = $order_id;
			$orderShippingEntity->shipping_date = $this->getPost ( 'shipping_date' );
			$orderShippingEntity->status = OrderShippingEntity::NORECEIV;
			$id = $orderShippingDataService->create ( $orderShippingEntity );
			
			//get order
			$dealEnity = $this->dealDataService->getById ( $dealOrderEntity->deal_id );
			$grouponEntity = $dealEnity->getGroupon ();
			//recipient
			$userDataService = new UserDataService ();
			$recipientEntity = $userDataService->getById ( $dealOrderEntity->user_id );
			$shippingerEntity = $userDataService->getById ( $this->getPost ( 'user_shipping' ) );
			$content = CONTENT_MAIL_SHIPPING;
			$content = str_replace ( '[order_id]', $dealOrderEntity->id, $content );
			$content = str_replace ( '[groupon_title]', $grouponEntity->groupon_name, $content );
			$content = str_replace ( '[order_quantity]', $dealOrderEntity->quantity, $content );
			$content = str_replace ( '[order_price]', $dealEnity->sale_price, $content );
			$content = str_replace ( '[order_total_price]', $dealOrderEntity->total_price, $content );
			$content = str_replace ( '[shipping_date]', $this->getPost ( 'shipping_date' ), $content );
			$content = str_replace ( '[shippinger_name]', $shippingerEntity->fullname, $content );
			$content = str_replace ( '[shippinger_id]', $shippingerEntity->id, $content );
			$content = str_replace ( '[shippinger_phone]', $shippingerEntity->phone, $content );
			$content = str_replace ( '[shippinger_image]', $shippingerEntity->getImage (), $content );
			//send mail
			try {
				$mail = new SENDMAIL ();
				$res = $mail->send ( $recipientEntity->email, $recipientEntity->fullname, 'Thông tin giao hàng từ muaHội.com', $content );
			} catch ( Exception $ex ) {
			
			}
			if ($id == $order_id) {
				$data = array ('success' => 1 );
			} else {
				$data = array ('success' => 0, 'msg' => 'Có lỗi xảy ra! Đơn hàng đã được giao.' );
			}
		} catch ( Exception $e ) {
			$data = array ('success' => 0, 'msg' => 'Có lỗi xảy ra! Đơn hàng đã được giao.' );
		}
		echo Zend_Json::encode ( $data );
		exit ();
	}
	public function confirmShippingAction() {
		$order_id = $this->getQuery ( 'id', 0 );
		$dealOrderEntity = $this->dealOrderDataService->getById ( $order_id );
		$orderAddressEntity = $this->orderAddressDataService->getById ( $dealOrderEntity->id );
		$dealEntity = $this->dealDataService->getById ( $dealOrderEntity->deal_id );
		$grouponEntity = $this->grouponDataService->getById ( $dealEntity->groupon_id );
		$orderShippingDataService = new OrderShippingDataService ();
		$orderShippingEntity = $orderShippingDataService->getById ( $dealOrderEntity->id );
		
		if ($dealOrderEntity->payment_method == PAYMENT_INVOICE) {
			$this->smarty->assign ( 'getMoney', $dealOrderEntity->total_price );
		}
		$this->userDataService = new UserDataService ();
		$this->smarty->assign ( 'dealOrderEntity', $dealOrderEntity );
		$this->smarty->assign ( 'orderAddressEntity', $orderAddressEntity );
		$this->smarty->assign ( 'dealEntity', $dealEntity );
		$this->smarty->assign ( 'grouponEntity', $grouponEntity );
		$this->smarty->assign ( 'orderShippingEntity', $orderShippingEntity );
		$this->smarty->assign ( 'userEntity', $this->userDataService->getById ( $orderShippingEntity->user_id ) );
		$this->smarty->display ( 'Admin/DealOrder/confirmShipping.tpl' );
	}
	public function doConfirmShippingAction() {
		$order_id = $this->getPost ( 'deal_order_id', 0 );
		$dealOrderEntity = $this->dealOrderDataService->getById ( $order_id );
		try {
			//set completee for shippinger
			//do shipping
			$dealOrderEntity->payment_status = DealOrderEntity::COMPLETED;
			$dealOrderEntity->order_status = DealOrderEntity::COMPLETED;
			$this->dealOrderDataService->update ( $dealOrderEntity );
			$data = array ('success' => 1 );
		} catch ( Exception $e ) {
			$data = array ('success' => 0, 'msg' => 'Có lỗi xảy ra! Đơn hàng đã được giao.' );
		}
		echo Zend_Json::encode ( $data );
		exit ();
	}
	public function staffShippingAction() {
		$user_id = $this->getQuery ( 'id', 0 );
		$param = array ();
		$param ['user_id'] = $user_id;
		if ($this->getPost ( 'submit', '' ) != '') {
			if ($this->getPost ( 'user_shipping' ) != - 1)
				$param ['user_id'] = $this->getPost ( 'user_shipping' );
			if ($this->getPost ( 'date', '' ) != '')
				$param ['date'] = $this->getPost ( 'date', '' );
			if ($this->getPost ( 'order_id', '' ) != '')
			$param ['order_id'] = $this->getPost ( 'order_id', '' );
		}
		$userDataService = new UserDataService ();
		$userEntities = $userDataService->getUser ( array ('userType' => ROLE_SHIPPING ) );
		$listItems = $this->dealOrderDataService->getShippingOfStaff ( $param );
		$this->smarty->assign ( 'userEntities', $userEntities );
		$this->smarty->assign ( 'listItems', $listItems );
		$this->smarty->assign ( 'user_id', $user_id );
		$this->smarty->assign ( 'pageName', 'shipping' );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/DealOrder/staffShipping.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
}
